package com.design_pattern.flyweight;

import com.peng.Bullet;

import java.util.ArrayList;
import java.util.List;

/**
 * 享元模式
 */
public class BulletPool {


    public static List<Bullet> bulletList = new ArrayList<>();

    static {
        for (int i = 0; i < 100; i++) {
            bulletList.add(new Bullet());
        }
    }

    public static Bullet getBullet() {

        for (Bullet bullet : bulletList) {
            if (!bullet.isLive()) {
                return bullet;
            }
        }
        return new Bullet();

    }


    public static void main(String[] args) {
        //享元模式和组合模式的结合使用，  例如  字符串的常量池 A和B   组合 AB   当多次使用时都是拿的一份共享数据
        String a = "abv";
        String b = "abv";
        String c = new String("abv");
        System.out.println(a == b);
        System.out.println(a == c);
        System.out.println(a == c.intern());
    }

}
